package com.jf.cloud.product.controller.multishop;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.product.dto.ProductSearchDTO;
import com.jf.cloud.common.product.vo.SkuVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.ExcelUtil;
import com.jf.cloud.product.model.SkuStock;
import com.jf.cloud.product.service.SkuService;
import com.jf.cloud.product.service.SkuStockService;
import com.jf.cloud.product.vo.SkuExcelVO;
import com.jf.cloud.product.vo.SkuStockVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController("multishopSkuController")
@RequestMapping("/m/sku")
@Tag(name = "multishop-sku信息")
public class SkuController {

    @Autowired
    private  SkuService skuService;
    @Autowired
    private SkuStockService skuStockService;

    @GetMapping("/get_all_skuList")
    @Operation(summary = "通过prodId获取全部的规格列表" , description = "通过prodId获取全部的规格列表")
    @Parameter(name = "prodId", description = "商品id" )
    public ServerResponseEntity<List<SkuVO>> getSkuListByProdId(Long prodId) {
        List<SkuVO> skus = skuService.listSkuWithAttrBySpuId(prodId);
        List<SkuStockVO> skuStockList = skuStockService.listStockBySpuId(prodId);
        Map<Long, Integer> stockMap = skuStockList.stream().collect(Collectors.toMap(SkuStockVO::getSkuId, SkuStockVO::getStock));
        for (SkuVO skuVO : skus) {
            if (stockMap.containsKey(skuVO.getSkuId())) {
                skuVO.setStock(stockMap.get(skuVO.getSkuId()));
            }
        }
        return ServerResponseEntity.success(skus);
    }

    @GetMapping("/get_stock_warning_num")
    @Operation(summary = "查询sku库存预警数量" , description = "查询sku库存预警数量")
    public ServerResponseEntity<Integer> getStockWarningNum(ProductSearchDTO product) {
        product.setShopId(AuthUserContext.get().getTenantId());
        Integer count = skuService.getStockWarningNum(product);
        return ServerResponseEntity.success(count);
    }

    @GetMapping("/sold_excel")
    @Operation(summary = "导出sku信息" , description = "导出sku信息")
    public ServerResponseEntity<Void> skuSoldExcel( HttpServletResponse response, ProductSearchDTO product) {
        product.setShopId(AuthUserContext.get().getTenantId());
        List<SkuExcelVO> skuList = skuService.exportSkuList(product);
        ExcelUtil.soleExcel(response, skuList, SkuExcelVO.EXCEL_NAME, SkuExcelVO.MERGE_ROW_INDEX, SkuExcelVO.MERGE_COLUMN_INDEX, SkuExcelVO.class);
        return ServerResponseEntity.success();
    }

    @GetMapping("/page_sku")
    @Operation(summary = "分页查询sku信息" , description = "分页查询sku信息")
    public ServerResponseEntity<PageVO<SkuVO>> pageSku(PageDTO pageDTO, ProductSearchDTO product) {
        product.setShopId(AuthUserContext.get().getTenantId());
        PageVO<SkuVO> skuPage = skuService.pageSku(pageDTO, product);
        List<SkuVO> skuList = skuPage.getList();
        if(CollUtil.isEmpty(skuList)){
            return ServerResponseEntity.success(skuPage);
        }
        for (SkuVO skuVO : skuList) {
            SkuStock skuStock = skuStockService.getBySkuId(skuVO.getSkuId());
            skuVO.setStock(skuStock.getStock());
        }
        return ServerResponseEntity.success(skuPage);
    }


}
